 /*
 * Name        : main.cpp
 * Author      : yinjianwen
 * Create      : 2018年1月2日
 * Version     :
 * Copyright   : Copyright © 1998 - 2017 Bluedon All Rights Reserved
 * cription : C++, Ansi-style
 */
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <bits/pthreadtypes.h>
#include <sched.h>
#include "class.h"

using namespace std;

void EagerSingleton(void);
void LazySingleton(void );
void * Lazytest(void *);

int main()
{
    //EagerSingleton();
    LazySingleton();
}
void EagerSingleton()
{
    SingletonHunger *p;
    p = SingletonHunger::GetInstance();
    printf("-> %d\n", p->Add(1,1));
    printf("%x\n", p);
    p->DestroySingleton();
    p = SingletonHunger::GetInstance();
    /**
    * delete放弃了内存块的所有权.
     * 但是在没有被覆盖篡改之前，仍然是可以被寻址的
     */
    printf("-> %d\n", p->Add(2, 3));
    printf("%x\n", p);
}
void LazySingleton()
{
    int i;
//    int old = 0, fresh = 0;
    pthread_t tid;
    sched_param para;
    pthread_attr_t attr;
    pthread_attr_init(&attr);
    pthread_attr_getschedparam(&attr, &para);
    para.sched_priority = 90;
    pthread_attr_setschedparam(&attr, &para);
    for (i = 0; i < 10; i++) {
        printf("---\n");
        pthread_create(&tid, &attr, Lazytest, NULL);
        usleep(100000);
//        fresh = Lazytest();
//        if (old != fresh && old != 0) {
//            printf("!!!!!!!!\n");
//        }
//        old = fresh;
    }
}
void * Lazytest(void *)
{
    //SingletonLazy *p = SingletonLazy::DoubleCheckGetInstance();
    SingletonLazy *p = SingletonLazy::MeyersGetInstance();
    //printf("- %d -\n", p->Add(1, 2));
    int addr;
    memcpy(&addr, &p, sizeof(p));
    printf("%d\n", addr);
    sleep(100);
    return NULL;
}
